package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicPositionGoodsParam;
import cn.stylefeng.guns.modular.hx.basic.model.result.HxBasicPositionGoodsResult;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicPositionGoodsService;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageStockResult;
import cn.stylefeng.guns.modular.hx.storage.model.vo.BasicPositionJhwVo;
import cn.stylefeng.guns.modular.hx.storage.model.vo.StoragePositionGoodsVo;
import cn.stylefeng.guns.modular.hx.storage.model.vo.StoragePositionVo;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hxStoragePositionCx")
public class HxStoragePositionCxController extends BaseController {

    private String PREFIX = "/hx/storage/storagePositionCx";

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicPositionService basicPositionService;

    @Autowired
    private HxBasicPositionGoodsService hxBasicPositionGoodsService;

    @Autowired
    private HxStorageStockService hxStorageStockService;

    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/storagePositionCx.html";
    }


    /**
     * 查询全部
     * @param param
     * @return
     */
    @RequestMapping("/listAll")
    @ResponseBody
    public List<List<BasicPositionJhwVo>> listAll(BasicPositionParam param) {
        List<StoragePositionVo> res = new ArrayList<>();
        //根据仓库ID，获取库位信息
        List<BasicPositionResult> basicPositionResults = basicPositionService.listJhwOrderByNum(param);

        //根据仓库ID，获取库位绑定商品信息
        HxBasicPositionGoodsParam q1 = new HxBasicPositionGoodsParam();
        if(ToolUtil.isNotEmpty(param.getWarehouseId())){
            q1.setWarehouseId(param.getWarehouseId().longValue());
        }
        List<HxBasicPositionGoodsResult> hxBasicPositionGoodsList = this.hxBasicPositionGoodsService.findListBySpec(q1);
        Map<String,List<HxBasicPositionGoodsResult>> hxBasicPositionGoodsMap = new HashMap<>();
        List<HxBasicPositionGoodsResult> positionGoodsList = null;
        for(HxBasicPositionGoodsResult obj : hxBasicPositionGoodsList){
            if(hxBasicPositionGoodsMap.get(obj.getPositionNumber()) == null){
                positionGoodsList = new ArrayList<>();
                positionGoodsList.add(obj);
                hxBasicPositionGoodsMap.put(obj.getPositionNumber(),positionGoodsList);
            }else{
                hxBasicPositionGoodsMap.get(obj.getPositionNumber()).add(obj);
            }
        }
        //根据仓库ID，获取库位正常库存信息
        HxStorageStockParam q2 = new HxStorageStockParam();
        if(ToolUtil.isNotEmpty(param.getWarehouseId())){
            q2.setWarehouseId(param.getWarehouseId().longValue());
        }
        List<HxStorageStockResult> hxStorageStockList = this.hxStorageStockService.getPositionKcnum(q2);
        Map<String,List<HxStorageStockResult>> hxStorageStockMap = new HashMap<>();
        List<HxStorageStockResult> _hxStorageStockList = null;
        for(HxStorageStockResult obj : hxStorageStockList){
            if(hxStorageStockMap.get(obj.getPositionNumber()) == null){
                _hxStorageStockList = new ArrayList<>();
                _hxStorageStockList.add(obj);
                hxStorageStockMap.put(obj.getPositionNumber(),_hxStorageStockList);
            }else{
                hxStorageStockMap.get(obj.getPositionNumber()).add(obj);
            }
        }
        //组装最终返回数据
        StoragePositionVo storagePositionVo = null;
        StoragePositionGoodsVo _storagePositionGoodsVo = null;
        for(BasicPositionResult obj : basicPositionResults){
            storagePositionVo = new StoragePositionVo();
            storagePositionVo.setPositionAreaName(obj.getPositionAreaName());
            storagePositionVo.setPositionNumber(obj.getPositionNumber());
            if(hxBasicPositionGoodsMap.get(obj.getPositionNumber()) != null){
                for(HxBasicPositionGoodsResult obj1 : hxBasicPositionGoodsMap.get(obj.getPositionNumber())){
                    _storagePositionGoodsVo = new StoragePositionGoodsVo();
                    _storagePositionGoodsVo.setGoodsName(obj1.getGoodsName());
                    _storagePositionGoodsVo.setGoodsNumber(obj1.getGoodsNumber());
                    _storagePositionGoodsVo.setZcKcNum(new BigDecimal(0));
                    if(hxStorageStockMap.get(obj.getPositionNumber()) == null){
                        for(HxStorageStockResult obj2 : hxStorageStockMap.get(obj.getPositionNumber()) ){
                            if(_storagePositionGoodsVo.getGoodsNumber().equals(obj2.getGoodsNumber())){
                                _storagePositionGoodsVo.setZcKcNum(new BigDecimal(obj2.getKcNum()));
                            }
                        }
                    }
                    storagePositionVo.getGoodsList().add(_storagePositionGoodsVo);
                }

            }
            res.add(storagePositionVo);
        }
        List<List<BasicPositionJhwVo>> rs = new ArrayList<>();
        List<BasicPositionJhwVo> _rs = null;
        BasicPositionJhwVo _basicPositionJhwVo = null;
        String _flag = "";
        for(StoragePositionVo obj : res){
            _basicPositionJhwVo = new BasicPositionJhwVo();
            _basicPositionJhwVo.setPositionNumber(obj.getPositionNumber());
            if(obj.getGoodsList().size() > 0){
                _basicPositionJhwVo.setType("1");
                if(obj.getGoodsList().size() > 1){
                    _basicPositionJhwVo.setType("2");
                }
                for(StoragePositionGoodsVo obj1 : obj.getGoodsList()){
                    if(obj1.getZcKcNum().compareTo(new BigDecimal(0)) == 1){
                        _basicPositionJhwVo.setType("3");
                    }
                }
            }
            if(_flag.equals(obj.getPositionNumber().substring(0,4))){
                _rs.add(_basicPositionJhwVo);
            }else{
                _rs = new ArrayList<>();
                _rs.add(_basicPositionJhwVo);
                rs.add(_rs);
                _flag = obj.getPositionNumber().substring(0,4);
            }
        }
        return rs;
    }

}
